package day12;

import java.util.Iterator;

public class MyArray implements/* List, */ Iterable{
	private Object[] os;
	private int size;

	public MyArray(){
		os = new Object[10];
	}
	public MyArray(int initSize){
		os = new Object[initSize];
	}
	public Iterator iterator() {
		return new Iterator(){
			private int index = 0;
			public boolean hasNext() {
				return index<size;
			}

			public Object next() {
				Object o = os[index];
				index++;
				return o;	
			}

			public void remove() {

			}
		};
	}
	public void add(Object o){
		if(size==os.length){
			expa();
		}
		os[size] = o;
		size++;
	}
	
	public void delete(int index){
		if(index>size || index<0){
			return;
		}
		//System.arraycopy(os, index+1, os, index, size-index);
		for(int i=index; i<size-1; i++){
			os[i] = os[i+1];
		}
		os[size-1]= null;
		size--;
	}
	
	public void set(int index, Object o){
		os[index] = o;
	}
	
	public Object get(int index){
		return os[index];
	}
	
	public int size(){
		return size;
	}
	
	private void expa() {
		int newSize = (int) (os.length*1.5);
		Object[] os1 = new Object[newSize]; 
		System.arraycopy(os, 0, os1, 0, os.length);
		os = os1;
	}
	
	public static void main(String[] args) {
		MyArray ma = new MyArray();
		ma.add("aaa");
		ma.add("bbb");
		ma.add("ccc");
		ma.add("ddd");
		ma.delete(1);
		ma.set(0, "aaaaaaaaaaaa");
		for(int i=0; i<ma.size(); i++){
			Object o = ma.get(i);
			System.out.println(o);
		}
		System.out.println("===========================");
		Iterator it = ma.iterator();
		while(it.hasNext()){
			Object o = it.next();
			System.out.println(o);
		}
	}

}




